var buttons;
var t_time;
var from_id = 0;
var flag_title = 0;
var flag_body = 0;
$("document").ready(function() {
	setInterval('updateClock()', 1000 )
	///Normal Keyboard Functions
	$('#topic_create').text('Tell your story');
	$('#topic_send').text('Send');
	$('#command').keyup(function(event) {
  		len_body = $(this).val().length;
  		$('#body_words').text("Body: " + len_body)
  		if (len_body >= 150) {
  			document.getElementById("body_words").style.color = "#66E275"
  			flag_body = 1
  			$('#body_words').text("Body: " + len_body)
  		} else {
  			document.getElementById("body_words").style.color = "#F56E8D"
  			flag_body = 0
  			var minus = 150 - len_body;
  			$('#body_words').text("Body: " + minus + " to go")
  		}
  		if (flag_body != 0) {
  			$('#topic_send').fadeIn('fast', function() {});
  		} else {
  			$('#topic_send').fadeOut('fast', function() {});
  		}
	});
});
//function reply_vote(vote,iid){
//        $.post("post.php", { vote : vote, iid : iid});
//}

///function submit_tweet(iid){
///       	var txt_area = "textarea#text" + iid; 
///		var text = $(txt_area).val();	
///		$.post("posttweet.php",{ text_content : text ,iid :iid });
	///	$.post("post.php", { vote: 1, iid : iid});	
		///hello = setInterval(load_tweets, 1500);
        ///document.getElementById(iid).style.display = 'none';
//}
//function show_textbox(){
//	$("#resultmessage").html("<div style='display:block; background-color: Green; color: White; font-size: 15px; font-weight: bold; padding: 0px 0px 0pxh0px;'>Saved!")
//	$("#resultmessage").show('slow');
//}
//the clock

function send_topic(id) {
	var body = document.getElementById("command").value;
	$.post("/json/fml", { id: id, body: body, lang: "en"},
		function(data){
			if (data != "") {
				toggle_txtbox();
				location.reload();
			} else {
				alert("Failed to deliver message !! Try again !");
			}
		});
}
function toggle_txtbox(){
	flag_title = 0;
	flag_body = 0;
	$('#body_words').text("At least 150 alphabets for title")
	$('#title_words').text("At least 20 alphabets for body")
	if ($("#topic_create").text() == "Tell your story") {
		$('#commandbox').fadeIn('fast', function() {});
		$('#title_words').fadeIn('fast', function() {});
		$('#body_words').fadeIn('fast', function() {});
		$('#topic_create').text('Close');
	} else if ($('#topic_create').text() == 'Close') {
		$('#command').val("")
		$('#input_title').val("")
		$('#title_words').fadeOut('fast', function() {});
		$('#body_words').fadeOut('fast', function() {});
		$('#commandbox').fadeOut('fast', function() {});
		$('#topic_create').text('Tell your story');
		$('#topic_send').fadeOut('fast', function() {});
	}
}

function updateClock ( )
{
  var currentTime = new Date ( );

  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );

  // Pad the minutes and seconds with leading zeros, if required
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

  // Choose either "AM" or "PM" as appropriate
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

  // Convert the hours component to 12-hour format if needed
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

  // Convert an hours component of "0" to "12"
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;

  // Compose the string for display
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

  // Update the time display
  document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}
